package com.karolak.stock.oscilator;

import com.karolak.stock.data.DataManager;
import com.karolak.stock.indicator.Indicator;

public class DeltaOsc extends Oscilator {

	private double threshold;

	public DeltaOsc(DataManager dataManager, Indicator input, int period, double threshold) {
		super(dataManager, period, input);
		this.threshold = threshold;
		setName(String.format("%s%s", super.getName(), getInput().getName()));
	}

	public double execute(double x) {
		double[] signal = getDataManager().getSeries(getInput().getName());

		if (signal.length > 1) {
			double v0 = signal[signal.length - 1];
			int ix = 0;
			do {
				double vn = signal[signal.length - 2 - ix];
				double delta = v0 - vn;
				if (delta > threshold) {
					return delta;
				}
				if (delta < -threshold) {
					return delta;
				}
				ix++;
			} while (ix < getPeriod() - 1 && ix < signal.length - 1);
		}
		return 0;
	}
}
